package com.imooc.sell.VO.wang;

import com.imooc.sell.VO.wang.InParamVO.OrderDetailVO;
import com.imooc.sell.VO.wang.InParamVO.OrderSaveVO;
import com.imooc.sell.controller.OrderController;
import com.imooc.sell.dataobject.OrderDetail;
import com.imooc.sell.dataobject.User;
import com.imooc.sell.dto.OrderDTO;
import com.imooc.sell.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
@Controller
@RequestMapping(value = "/test")
public class ServiceTest {
    @Autowired
    OrderService orderService;

    @Test
    public void testUser() {
        User user = orderService.getUserBaseInfo("1");
        System.out.println(user.toString());
    }

    @Test
    public void testSave() {
        OrderSaveVO orderDTO = new OrderSaveVO();
        orderDTO.setBuyer_name("冯利");
        orderDTO.setBuyer_phone("18422221111");
        orderDTO.setBuyer_address("20号桌");
        orderDTO.setBuyer_openid("1");
        orderDTO.setOrder_amount(new BigDecimal(48.5));
        List<OrderDetailVO> orderDetailList = new ArrayList<>();

        OrderDetailVO orderDetail1 = new OrderDetailVO();
        orderDetail1.setProduct_id("1");
        orderDetail1.setName("干锅里脊");
        orderDetail1.setPrice(new BigDecimal("20.5"));
        orderDetail1.setQuantity(1);
        orderDetail1.setProductIcon("http:xxx.com");

        OrderDetailVO orderDetail2 = new OrderDetailVO();
        orderDetail2.setProduct_id("2");
        orderDetail2.setName("干锅牛蛙");
        orderDetail2.setPrice(new BigDecimal("14"));
        orderDetail2.setQuantity(2);
        orderDetail2.setProductIcon("http:xxx.com");

        orderDetailList.add(orderDetail1);
        orderDetailList.add(orderDetail2);

        orderDTO.setFoods(orderDetailList);

        OrderController orderController = new OrderController();
        orderController.orderSave(orderDTO);
        System.out.println("完成");
    }

    @Test
    public void testCancle() {
        OrderDTO orderDTO = new OrderDTO();
        orderDTO.setOrderId("2049748904");
        orderService.cancel(orderDTO);
    }

    @Test
    public void testPaid() {
        orderService.paid("2049748904");
    }

    @Test
    public void testPayBack() {
        orderService.payBack("2049748904");
    }

    @Test
    public void testFindOneOrder() {
        System.out.println("查询单个订单" + orderService.findOneOrder("2049748904"));
    }

    @Test
    public void testFindList() {
        PageRequest pageRequest = new PageRequest(0, 1);
        Page<OrderDTO> page = orderService.findOrderList("1", pageRequest);
        Assert.assertNotNull(page.getTotalElements());
    }

    @RequestMapping(value = "/testModel")
    public ModelAndView testmav(Model model) {
        //System.out.println("***********************进来了********************************");
        User user = new User();
        user.setName("wang");
        model.addAttribute("user", user);
        return new ModelAndView("/wang/index");
    }

    @Test
    public void testOrder() {
        OrderDTO resultDTO1 = orderService.findOneOrderMaster("1111");
        log.info(resultDTO1.toString());
    }
}
